/*
 * TUtils
 * Petko D. Petkov; GNUCITIZEN; http://www.gnucitizen.org
 */

function TSerializer(store) {
	var doc = document.implementation.createDocument('', 'store', null);
	var triples = store.search();

	for (var i = 0; i < triples.length; i++) {
		var triple = doc.createElement('triple');
		triple.setAttribute('subject', triples[i].subject);
		triple.setAttribute('predicate', triples[i].predicate);
		triple.setAttribute('object', triples[i].object);

		doc.firstChild.appendChild(doc.createTextNode("\n"));
		doc.firstChild.appendChild(triple);
		doc.firstChild.appendChild(doc.createTextNode("\n"));
	}

	var serializer = new XMLSerializer();
	return serializer.serializeToString(doc);
}

function TDeserializer(source) {
	var store = new TStore;
	var domParser = Components.classes['@mozilla.org/xmlextras/domparser;1']
		.createInstance(Components.interfaces.nsIDOMParser);

	var doc = domParser.parseFromString(source, 'text/xml');
	var triples = doc.evaluate('/store/triple', doc, null, 0, null);

	for (var triple = null; (triple = triples.iterateNext()); )
		store.insert({
			subject: triple.getAttribute('subject'),
			predicate: triple.getAttribute('predicate'),
			object: triple.getAttribute('object')});

	return store;
}
